/*********************************************************************************/
/*                                                                               */
/*???:??DS18B20??????                                                */                               
/*                                                                               */
/*????:?????DALLS????????????DS18B20,??????,       */
/*          ???????,??????????                                 */
/*                                                                               */
/*                                                                               */
/*                                                                               */
/*                                                       ??:  12M       */
/*                                                                               */
/*********************************************************************************/
#include <at89x51.h>

sbit DQ =     P3 ^ 7;  //????DQ
sbit led1    =P2^0;
sbit led2    =P2^1;
sbit led3    =P2^2;
sbit led4    =P2^3;
#define uchar unsigned char
uchar one;
unsigned char tx[10]={0,0,0x2E,0,0,0,0,0xDF,0x43,0x0A};
unsigned code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
                        0xf8,0x80,0x90};
/******************************************************************************/
void Delay(int num)//????
{
	while(num--) ;
}
/******************************************************************************/
void Delaynms(unsigned int di) //??
{
	unsigned int da,db;
 	for(da=0;da<di;da++)
   		for(db=0;db<100;db++);
}

/******************************************************************************/

void TX(unsigned char TX_char)
{
  SBUF=TX_char;
  while(!TI);
  TI=0;

}
/******************************************************************************/	
void Init_DS18B20(void)//???ds1820
{
	unsigned char x=0;
	DQ = 1;    //DQ??
	Delay(8);  //????
	DQ = 0;    //????DQ??
	Delay(80); //???? ?? 480us
	DQ = 1;    //????
	Delay(14);
	x=DQ;      //????? ??x=0?????? x=1??????
	Delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void)//?????
{
	unsigned char i=0;
	unsigned char dat = 0;
	for (i=8;i>0;i--)
	{
		DQ = 0; // ?????
		dat>>=1;
		DQ = 1; // ?????
		if(DQ)
		dat|=0x80;
		Delay(4);
	}
	return(dat);
}

/******************************************************************************/
void WriteOneChar(unsigned char dat)//?????
{
	unsigned char i=0;
	for (i=8; i>0; i--)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay(2);
		DQ = 1;
		dat>>=1;
	}
}

/******************************************************************************/
void ReadTemperature(void)//????
{
	unsigned char a=0;
	unsigned char b=0;
	unsigned char Data_L=0;
	unsigned char num=0;
	Init_DS18B20();
	WriteOneChar(0xCC); // ??????????
	WriteOneChar(0x44); // ??????
	Init_DS18B20();
	WriteOneChar(0xCC); //??????????
	WriteOneChar(0xBE); //???????
//	Delay(5000);
	a=ReadOneChar();  //??8?
	b=ReadOneChar(); //??8?

    tx[0] = (a/16+b*16)/10;      //????


	tx[1] = (a/16+b*16)%10; 

    Data_L=a&0X0F;
     for(num=3;num<7;num++)		 //????
   {
      Data_L=Data_L*10;				 //10	   //100		//40		 //80
     tx[num]=Data_L/16;			 //0	  //6			//2			 //5
      Data_L=Data_L%16;				 //10	   //4			//8
	}

}
/******************************************************************************/
void Int_232(void)	//232???
{
  TMOD=0x20;
  SCON=0x50;
  TH1=0xFD;
  TL1=0xFD;
  TR1=1;
  TI=0;
}

void Display_outtemp(void)
{
   unsigned char a;
   for(a=0;a<=50;a++)
   {
   P0=table[tx[0]];
		for(one=0;one<8;one++)
   tx[0]=_crol_(tx,1);
   Delaynms(5);
   P2_0 = 1;

   P0=(table[tx[1]])&0x7f;
   P2_1 = 0;
   Delaynms(5);
   P2_1 = 1;

   P0=table[tx[3]];
   P2_2 = 0;
   Delaynms(5);
   P2_2 = 1;

    P0=table[tx[4]];
   P2_3 = 0;
   Delaynms(5);
   P2_3 = 1;
   }
}

/******************************************************************************/

void main(void)
{  	
 
  Int_232();


while(1)
{  

	ReadTemperature();	 //????
	Display_outtemp();

} 	



}
